package com.atfcm.stack;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author 96534
 * @create 2025/3/26 9:33
 */
public class T_946_验证栈序列 {
    // pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Deque<Integer> stack = new LinkedList<>();
        int j = 0;
        for(int i = 0; i <pushed.length ; i++) {
            stack.push(pushed[i]);
            while(!stack.isEmpty() &&  j < popped.length && stack.peek() == popped[j] ) {
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();

    }
}
